package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequirePermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }

    @RequestMapping("/activity")
    public String activity(Model model, Long id, @ModelAttribute("title") String title){
        //先判断当前用户是否已经投票
        Employee currentUser = UserContext.getCurrentUser();
        //根据员工id和投票信息id联表查询选择关系表
        Long relationId = activityService.selectRelation(currentUser.getId(),id);
        if (relationId != null){
            //已经投过票,直接转发到结果页面
            return "forward:/activity/result";
        }

        //还未投票则投票页面
        List<ActivityItem> activityItems = activityService.selectItemByActivityId(id);
        model.addAttribute("activityItems",activityItems);
        return "activity/activity";
    }

    @RequestMapping("/result")
    public String result(Model model,Long optionsRadios,@ModelAttribute("title") String title){
        //判断是否已经投过票,投过票则为null(list页面直接转发过来的),未投过票表示是从投票页面转过来
        if(optionsRadios != null){
            //投票以后先给投票人数加一
            activityService.addAINumById(optionsRadios);

            //投票结果保存到中间表
            Employee currentUser = UserContext.getCurrentUser();
            activityService.saveRelation(currentUser.getId(),optionsRadios,new Date());
        }

        //获取该投票项目的投票总数
        int sum = activityService.selectNumCount(title);
        model.addAttribute("sum",sum);

        //查询这个title下的所有选项以及选项人数
        List<ActivityItem> activityItems = activityService.selectAINumByATitle(title);
        model.addAttribute("activityItems",activityItems);
        return "activity/result";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "投票删除",expression = "activity:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            activityService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "投票新增/编辑",expression = "activity:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Activity activity){
        if (activity.getId() != null) {
            activityService.update(activity);
        }else {
            activityService.save(activity);
        }
        return new JsonResult();
    }
}
